package com.cloudkinto.appletController.order;


import com.cloudkinto.appletservice.order.AppletOrderRakutenService;
import com.cloudkinto.appletservice.order.vo.AppletExport2ShipOrderReq;
import com.cloudkinto.appletservice.order.vo.rakuten.AppletOrderRakutenDetailResponse;
import com.cloudkinto.appletservice.order.vo.rakuten.AppletOrderRakutenListRes;
import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.constant.SysConstant;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.order.vo.OrderRakutenSysTipsReq;
import com.cloudkinto.service.orderoutput.vo.OrderOutputAddReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张永远
 * @since 2020-06-03
 */
@RestController
@Api(tags = "小程序乐天订单模块")
@RequestMapping("/api/applet/rakuten/")
public class AppletOrderRakutenController extends BaseController {

    @Autowired
    private AppletOrderRakutenService service;


//    @UserSessionRequired
//    @GetMapping("ordersync")
//    @ApiOperation(value = "同步订单", notes = "同步订单")
//    @CacheEvict(cacheNames = {"rakutendetail", "rakutenListpage"})
//    public SingleResult orderSync(String dateStart, String dateEnd, String dateType, int shopId) {
//        int rows = service.orderSync(dateStart, dateEnd, dateType, shopId);
//        return SingleResult.success();
//    }

    @UserSessionRequired
    @GetMapping("ordersyncInit")
    @ApiOperation(value = "同步订单初始化", notes = "同步订单初始化")
    public SingleResult ordersyncInit() {
        return SingleResult.success(service.ordersyncInit(getCurrentUserId(), getCompanyId()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
//    @Cacheable(value = "rakutendetail", key = "'rakutendetail'+#id")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AppletOrderRakutenDetailResponse.class)})
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }


    /**
     * String dateStart = request.getParameter("dateStart");//订单创建/付款 开始时间
     * String dateEnd = request.getParameter("dateEnd");//订单创建/付款 结束时间
     * String size = request.getParameter("pageSize");
     * String current = request.getParameter("current");
     * String keyword = request.getParameter("keyword");//关键字
     * String shipType = request.getParameter("shipmentType");//发货类型 1 次日达 0 普通
     * String dateType = request.getParameter("dateType"); //订单时间  1创建时间   3付款时间
     * String orderProgress = request.getParameter("orderProgress"); //订单状态
     * String isSend = request.getParameter("isSend");//是否转发货 0 未转 1已转
     * String shopId = request.getParameter("shopId"); //店铺id
     *
     * @return
     */
    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiResponses({@ApiResponse(code = 200, message = "", response = AppletOrderRakutenListRes.class)})
//    @Cacheable(value = "rakutenListpage",key ="'rakutenListpage'+#dateStart+#dateEnd+#keyword+#shipmentType+#dateType+#orderProgress+#isSend+#shopId+#pageSize+#current" )
    public PageResult<AppletOrderRakutenListRes> listPage(String dateStart, String dateEnd, String keyword, String shipmentType, String dateType,
                                                          String orderProgress, String isSend, String shopId, String pageSize, String current) {
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();

        if (!StringUtils.isEmpty(isSend))
            map.put("isSend", isSend);
        if (!StringUtils.isEmpty(orderProgress))
            map.put("orderProgress", orderProgress);
        if (!StringUtils.isEmpty(shipmentType))
            map.put("shipType", shipmentType);
        if (!StringUtils.isEmpty(keyword))
            map.put("keyword", keyword);
        if (companyId != null)
            map.put("companyId", companyId + "");
        if (!StringUtils.isEmpty(shopId)) {
            map.put("shopId", shopId);
        } else if (getShopId() != null) {
            map.put("shopId", getShopId() + "");
        }
        map.put("current", current);
        map.put("pageSize", pageSize);
        map.put("dateStart", dateStart);
        map.put("dateEnd", dateEnd);
        map.put("dateType", dateType);
        return service.getListPage(map);
    }

    /**
     * 把乐天订单组合成发货单
     *
     * @param orderid
     * @return
     */
    @UserSessionRequired
    @PostMapping("export2ShipOrder")
    @ApiOperation(value = "发货", notes = "乐天订单发货")
    public SingleResult export2ShipOrder(@RequestBody AppletExport2ShipOrderReq req) {
        Long companyId = getCompanyId();
        String[] orderIds = new String[]{req.getOrderid()};
        if (req.getIsSolo() == 0) {
            return service.queryOrder2SendOut(orderIds, getCurrentUserId(), true, companyId);
        } else if (orderIds.length > 0) {
            return service.queryOrder2SendOutNoPackage(req.getOrderid(), getCurrentUserId(), true, companyId);
        }
        return SingleResult.failure(SysConstant.NO_DATA);
    }

    @UserSessionRequired
    @PostMapping("updateTips")
    @ApiOperation(value = "修改备注信息", notes = "修改备注信息")
    public SingleResult updateSysTips(@RequestBody OrderRakutenSysTipsReq sysTipsReq) {
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();
        return service.updateSysTips(sysTipsReq, userId, companyId);
    }
    @UserSessionRequired
    @GetMapping("updateTipsInit")
    @ApiOperation(value = "修改备注信息初始化", notes = "修改备注信息初始化")
    public SingleResult updateSysTipsInit(Long orderid) {
        Long companyId = getCompanyId();
        Long userId = getCurrentUserId();
        return service.updateSysTipsInti(orderid);
    }

    /**
     * 把设置订单已转发货
     *
     * @param orderid
     * @return
     */
    @UserSessionRequired
    @PostMapping("setOrderSend")
    @ApiOperation(value = "标记为处理", notes = "标记为处理")
    public SingleResult setOrderSend(@RequestBody Long orderid) {
        Long userId = getCurrentUserId();
        Long[] orderids = new Long[]{orderid};
        int count = service.setOrderSend(orderids, userId);
        if (count == orderids.length) {
            return SingleResult.success();
        } else {
            return SingleResult.failure(SysConstant.FAILURE_COUNT, orderids.length - count);
        }
    }

}

